import { mapMutations } from 'vuex'
import { ERR_OK } from '@/api/options/statusCode'
import {Toast} from 'mint-ui'
import logo from '@/commom/default/bpm.jpg'
import dingtalk from 'dingtalk-javascript-sdk'
import {getDingTalkInfo} from '@/api/dingTalk'
import DingtalkEnv from 'dingtalk-javascript-env'
const LoginMixin = {
  data() {
    return {
      img: logo,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: '' }],
        password: [{ required: true, trigger: 'blur', validator: '' }]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
      redirect: undefined
      // baseUrl: getBaseUrl()
    }
  },
  created() {
  },
  methods: {
    ...mapMutations({
      setAlLChecked: 'SET_ALL_CHECKED_DEPART'
    }),
    // 获取签名
    getDingTalkInfo(option) {
      return new Promise((resolve, reject) => {
        getDingTalkInfo(option).then((res) => {
          this.loading = false
          console.log(res, 'res-------')
          const data = res.data
          if (res.code === ERR_OK) {
            console.log(DingtalkEnv, 'DingtalkEnv')
            if (DingtalkEnv.isDingTalkPC) { // 是否pc端
                this.getDingTalkReadyPc(data.corpId)
            } else if (DingtalkEnv.isDingTalk && (DingtalkEnv.isWebAndroid || DingtalkEnv.isWebiOS || DingtalkEnv.platform == "iPad")) {
              this.getDingTalkReady(data.corpId)
            }
          }
          resolve(res)
        }).catch(error => {
          this.loading = false
          reject(error)
        })
      })
    },

    // 免登录验证 1 自动登录 2 钉钉推送消息 3 钉钉发布的应用
    // 移动端------
    getDingTalkReady(corpId) {
      let that = this
      return new Promise(function(resolve, reject) {
        dingtalk.ready(function() {
          const dd = dingtalk.apis
          dd.runtime.permission.requestAuthCode({
            corpId: corpId,
            onSuccess(result) {
              const name = that.$route.name
              switch (name) {
                case 'Login':
                  that.getDingTalkValidate(result)
                  break
                case 'DingTalkMsg':
                  that.getDingTalkMsgValidate(result)
                  break
                case 'DingAppLogin':
                  that.getDingTalkAppValidate(result)
                  break
              }
            },
            onFail(err) {
              console.error(err, 'err')
              let instance = Toast({
                // message: that.$t('dingtalk.error'),
                message: err.errorMessage,
                iconClass: 'icon el-icon-error'
              })
              setTimeout(() => {
                instance.close()
              }, 2000)
              that.loading = false
              reject(err)
            }
          })
        })
        dingtalk.error(function(err) {
          // 钉钉验证出错
          console.log(err, '钉钉验证出错----')
          let instance = Toast({
            message: err.errorMessage,
            iconClass: 'icon el-icon-error'
          })
          setTimeout(() => {
            instance.close()
          }, 2000)
          that.loading = false
        })
      })
    },
    // pc端-----
    getDingTalkReadyPc(corpId) {
      let that = this
      return new Promise(function(resolve, reject) {
        // DingTalkPC.ready(function(res) {
          DingTalkPC.runtime.permission.requestAuthCode({
            corpId: corpId,
            onSuccess(result) {
              const name = that.$route.name
              switch (name) {
                case 'Login':
                  that.getDingTalkValidate(result)
                  break
                case 'DingTalkMsg':
                  that.getDingTalkMsgValidate(result)
                  break
                case 'DingAppLogin':
                  that.getDingTalkAppValidate(result)
                  break
              }
            },
            onFail(err) {
              console.error(error)
              let instance = Toast({
                message: err.errorMessage,
                iconClass: 'icon el-icon-error'
              })
              setTimeout(() => {
                instance.close()
              }, 2000)
              that.loading = false
              reject(err)
            }
          })
        // })
        DingTalkPC.error(function(err) {
          // 钉钉验证出错
          console.log(err, '钉钉验证出错---')
          let instance = Toast({
            message: err.errorMessage,
            iconClass: 'icon el-icon-error'
          })
          setTimeout(() => {
            instance.close()
          }, 2000)
          that.loading = false
        })
      })
    }
  },
  computed: {
    autoLogin: {
      get() {
        return this.$store.getters.autoLogin
      },
      set(value) {
        this.$store.commit('SET_AUTO_LOGIN', value)
      }
    },
    baserApi() {
      return this.$store.getters.baserApi
    }
  }
}
export default LoginMixin
